package com.push.controller;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * @Author： yu.Tan
 * @Date: 2020/9/8 17:37
 * @Description:
 */
@RestController
@RequestMapping("/")
public class PushController {
    /**
     * 微信测试账号推送
     */
    @RequestMapping("/push")
    public void push() {
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx80d3317081d39f40");//appid
        wxStorage.setSecret("4cd43fb8664340d5b4aed128d122b62d");//appsecret
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("ofuxIwDKv_AQd-32yMzhCMyB_8VI")//要推送的用户openid
                .templateId("ncooK_u20-7QDg5wRYM4buNjkmoVcD_yi7LdyLOVAeI")//模板id
                .url("https://www.jianshu.com/p/65a650c19aa1")//点击模板消息要访问的网址
                .build();
        //3,如果是正式版发送消息，，这里需要配置你的信息

        //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
        //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功:" + msg);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }


    }

    /**
     * 测试内网穿透
     *
     * @return
     */
    @RequestMapping("/test")
    public String testNetwork() {
        return "你好";
    }

    @RequestMapping("/wx_token")
    public void testWxToken(HttpServletRequest request) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        System.out.println(signature);
        System.out.println("test");

    }

    @RequestMapping("/send_msg")
    public void sendMsg(HttpServletRequest request) {
        //https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN


        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx75f8f1e4bdff0945");//appid
        wxStorage.setSecret("a6ac5e13f2c38e634c4bb4cdfe22eb0d");//appsecret
        WxMpService wxMpService = new WxMpServiceImpl();

        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("okxLi6Fg3gwt-uwLGYozd5uIvijQ")//要推送的用户openid
                .templateId("lV8RP2Zrlwsm1GkYLqnKPmD9KJUmEU_q1h2tWHZp9Ms")//模板id
                .url("https://www.jianshu.com/p/65a650c19aa1")//点击模板消息要访问的网址
                .build();

        //3,如果是正式版发送消息，，这里需要配置你的信息

        templateMessage.addData(new WxMpTemplateData("first", "你有新消息了", "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("company", "yes", "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("result", "good", "#FF00FF"));

        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功:" + msg);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }


    @RequestMapping("/send_msg_a")
    public void sendMsgA(HttpServletRequest request) {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx75f8f1e4bdff0945");  //appID
        wxStorage.setSecret("a6ac5e13f2c38e634c4bb4cdfe22eb0d");//appsecretID
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);


        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "亲，请记得收货。"),
                new WxMpTemplateData("company", "微信点餐"),
                new WxMpTemplateData("time", "18868812345"),
                new WxMpTemplateData("result", "微信点餐")
        );


        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("okxLi6Fg3gwt-uwLGYozd5uIvijQ")//要推送的用户openid
                .data(data) //数据
                .templateId("lV8RP2Zrlwsm1GkYLqnKPmD9KJUmEU_q1h2tWHZp9Ms")//模版id
                /*  .url("https://www.baidu.com/")//点击模版消息要访问的网址*/
                .build();


        //发起推送
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功：" + msg);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }

    }

    @RequestMapping("/send_msg_b")
    public void sendMsgB() {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx75f8f1e4bdff0945");  //appID
        wxStorage.setSecret("a6ac5e13f2c38e634c4bb4cdfe22eb0d");//appsecretID
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);


        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "亲，你好啊。"),
                new WxMpTemplateData("sender", "谭羽"),
                new WxMpTemplateData("revcer", "谭羽"),
                new WxMpTemplateData("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))),
                new WxMpTemplateData("result", "very nice！！！")
        );


        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("okxLi6Fg3gwt-uwLGYozd5uIvijQ")//要推送的用户openid
                .data(data) //数据
                .templateId("SH3HSLRR-a34rD4B0b8DYTuWx_Z2d7lCcjbKopUh9JQ")//模版id
                /*  .url("https://www.baidu.com/")//点击模版消息要访问的网址*/
                .build();


        //发起推送
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功：" + msg);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }

    }

    @RequestMapping("/send_msg_c")
    public void sendMsgC() {
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId("wx75f8f1e4bdff0945");  //appID
        wxStorage.setSecret("a6ac5e13f2c38e634c4bb4cdfe22eb0d");//appsecretID
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);


        //数据
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "亲，你好啊。"),
                new WxMpTemplateData("sender", "谭羽"),
                new WxMpTemplateData("revcer", "文光君"),
                new WxMpTemplateData("time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))),
                new WxMpTemplateData("result", "very nice！！！")
        );


        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("okxLi6J3AhHVDRFXzVqSazywAtMA")//要推送的用户openid
                .data(data) //数据
                .templateId("SH3HSLRR-a34rD4B0b8DYTuWx_Z2d7lCcjbKopUh9JQ")//模版id
                /*  .url("https://www.baidu.com/")//点击模版消息要访问的网址*/
                .build();


        //发起推送
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("推送成功：" + msg);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }

    }
}
